package com.huntero.hweather.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.client.HttpResponseException;

import android.text.TextUtils;
import android.util.Log;

public class HttpHelper {

	public static String get(String urlString,boolean output) throws IOException{
		Log.i("Huntero", "http get url="+urlString);
		StringBuffer result =null;
			
		URL url;
		try {
			url = new URL(urlString);
		
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET"); 
			conn.setDoInput(true);
			conn.setDoOutput(output);
			conn.setUseCaches(false);
			conn.setConnectTimeout(5*1000);
			
			int code = conn.getResponseCode() ;
			Log.i("Huntero", "get resp code="+code);
			if(code != 200)
				throw new HttpResponseException(code, "http response exception");
			
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String buffer=null;
			result = new StringBuffer();
			while((buffer = br.readLine()) != null){
				if(!TextUtils.isEmpty(buffer))
					result.append(buffer);
			}
			
			br.close();
			conn.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		Log.i("Huntero", "http get result="+result);
		return result == null?null:result.toString();
	}
	
	/**
	 * ping，检查网络状态
	 * @param ipOrHost
	 * @return
	 */
	public static boolean Ping(String ipOrHost){
		final String ip = ipOrHost;
		if(TextUtils.isEmpty(ip))return false;
		try {
			Process pc = Runtime.getRuntime().exec("ping -c 1 -w 20 " + ip);	//c:次数；w:等待超时时间，以秒为单位
			InputStream in = pc.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String line = null;
			StringBuffer sb = new StringBuffer();
			
			while((line = reader.readLine()) != null){
				sb.append(line);
			}
			reader.close();
			in.close();
			
			int status = pc.waitFor();
			
			Log.i("Huntero", "http network result="+(status == 0?"OK":"NO"));

			if(status == 0){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
//	public static final String WEATHER_URL = "http://www.weather.com.cn/data/sk/101090101.html";
//	public static final String WEATHER_URL_BD = "http://www.baidu.com/home/xman/data/superload?type=weather";
	
//	public static BDWeather getObjectByBD(String urlString){
//		BDWeather w=null;
//		try{
//			String origin=get(HttpHelper.WEATHER_URL_BD);
//			if(TextUtils.isEmpty(origin))return new BDWeather();
//			JSONObject json = new JSONObject(origin);
//			JSONObject data = json.getJSONObject("data");
//			JSONObject weather = data.getJSONObject("weather").getJSONObject("content");
//			
//			w = new BDWeather();
//			w.week = weather.getString("week");
//			w.city = weather.getString("city");
//			JSONObject today = weather.getJSONObject("today");
//			w.condition=today.getString("condition");
//			w.dayIndex="today";
//			w.pm25=today.getString("pm25");
//			w.temp=today.getString("temp");
//			w.wind=today.getString("wind");
//					
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		return w;
//	}
//	/**
//	 * {"weatherinfo":
//	 * 		{
//	 * 		 "city":"石家庄",
//	 * 		 "cityid":"101090101",
//	 * 		 "temp":"34",
//	 * 		 "WD":"东南风",
//	 * 		 "WS":"2级",
//	 * 		 "SD":"48%",
//	 * 		 "WSE":"2",
//	 * 		 "time":"15:40",
//	 * 		 "isRadar":"1",
//	 * 		 "Radar":"JC_RADAR_AZ9311_JB"
//	 * 		}
//	 * }
//	 * */
//	public static Weather getObject(String urlString){
//		Weather w = null;
//		try {
//			String origin=getWithUrl(HttpHelper.WEATHER_URL,false);
//			if(TextUtils.isEmpty(origin)) return new Weather();
//			
//			JSONObject weather = new JSONObject(origin);
//			JSONObject json = weather.getJSONObject("weatherinfo");
//			w = new Weather();
//			w.city=json.getString("city");
//			w.cityid=json.getString("cityid");
//			w.temp=json.getString("temp");
//			w.wd=json.getString("WD");
//			w.ws=json.getString("WS");
//			w.sd=json.getString("SD");
//			w.wse=json.getString("WSE");
//			w.time=json.getString("time");
//			w.isRadar=json.getString("isRadar");
//			w.radar=json.getString("Radar");
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//		return w;
//	}
//	public static String get(String urlString){
//		Log.i("Huntero", "url: "+urlString);
//		String result = null;
//		try {
//
//			HttpGet get = new HttpGet(urlString);
//			get.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36");
//			get.addHeader("Cookie","BAIDUID=FA8D86012A33B52CA7EB7626AB9784FE:FG=1; BDUSS=9FeTBoUUpSbmh4UGZOSjl2bGVIVG5HLTNqVG1OTmVVTGliaE9uS2F4bzNFMkZUQVFBQUFBJCQAAAAAAAAAAAEAAAAqy~wUZ2JoX2hlYXZlbgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADeGOVM3hjlTTU; BD_CK_SAM=1; H_PS_PSSID=7733_7509_1466_7650_7570_7802_6997_7730_6506_6018_7693_7673_7253_7607_7633_6888_7835_7415_7687_7710_7803_7474");
//			HttpResponse resp = new DefaultHttpClient().execute(get);
//			int code = resp.getStatusLine().getStatusCode();
//			Log.i("Huntero", "resp code="+code);
//			if(code != 200)
//				return null;
//			result = EntityUtils.toString(resp.getEntity(),"UTF-8");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		Log.i("Huntero", "http get="+result);
//		return result;
//	}
	
}


















